#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        WaitforElementById(driver,"com.lenovo.leos.appstore:id/tab_btn4",30).click()
    except NoSuchElementException:
        raise NoSuchElementException

    try:
        WaitforElementById(driver,"com.lenovo.leos.appstore:id/btnUninstall",30).click()
    except NoSuchElementException:
        raise NoSuchElementException

    try:
        WaitforElementById(driver,"com.lenovo.leos.appstore:id/download_num",30).click()
    except NoSuchElementException:
        raise NoSuchElementException


    time.sleep(2)
    while True:
        try:
            tasks=WaitforElementsById(driver,"com.lenovo.leos.appstore:id/bg_layer",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementById(driver,'com.lenovo.leos.appstore:id/popbtn_delete',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                clear_btn.click()
            time.sleep(2)
    driver.back()


def clickappdown(driver):
    try:
        tasks=WaitforElementsById(driver,'com.lenovo.leos.appstore:id/app_item_layout',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in tasks:
            try:
                size=WaitforElementById(btn,"com.lenovo.leos.appstore:id/app_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size,float(size[0:-1])
            if 'G' not in size:
                if 'K' in size or float(size[0:-1])<10:
                    continue
            btn.click()
            time.sleep(1)
            try:
                down_btn=WaitforElementById(driver,'com.lenovo.leos.appstore:id/leTextProgressBar',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                down_btn.click()
                waittime=60
                while waittime:
                    try:
                        text=WaitforElementById(down_btn,'com.lenovo.leos.appstore:id/app_detail_download_status',5).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0%' not in text:
                        try:
                            dele_btn=WaitforElementById(driver,'com.lenovo.leos.appstore:id/collectApp',10)
                        except NoSuchElementException:
                            down_btn.click()
                        else:
                            dele_btn.click()
                        time.sleep(2)
                        driver.back()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            try:
                                dele_btn=WaitforElementById(driver,'com.lenovo.leos.appstore:id/collectApp',10)
                            except NoSuchElementException:
                                down_btn.click()
                            else:
                                dele_btn.click()
                            time.sleep(2)
                            driver.back()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"

def game(driver):
    try:
        WaitforElementById(driver,"com.lenovo.leos.appstore:id/tab_btn2",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'休闲益智',u'消除游戏',u'打飞机',u'跑酷游戏',u'动作游戏',u'格斗游戏',u'射击游戏',u'竞速游戏',u'棋牌桌游',u'斗地主',u'网络游戏',u'卡牌游戏',u'角色扮演',u'经营养成',u'塔防游戏',u'策略游戏',u'体育游戏',u'音乐节奏']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    #driver.back()

def soft(driver):
    try:
        WaitforElementById(driver,"com.lenovo.leos.appstore:id/tab_btn3",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'影视.视频',u'音乐.铃声',u'新闻.阅读',u'拍摄.美图',u'聊天.社交',u'购物.优惠',u'生活服务',u'母婴.儿童',u'医疗.健康',u'运动.健身',u'办公.效率',u'考试.学习',u'地图.出行',u'实用.工具',u'理财.金融',u'桌面.美化',u'智能硬件',u'系统.优化',u'壁纸']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    #driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.lenovo.leos.appstore'    # apk的包名
    desired_caps['appActivity'] = '.activities.Main'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()
